// pages/index/index.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    xList: [],        // x轴方向的偏移
    currentX: [],     // 当前x的值
    list: [0, 1, 2, 3, 4]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.list.forEach((item, index) => {
      this.data.xList[index] = 0
    });
  },
  /**
   *  删除方案
   */
  handleMovableChange(e) {
    // console.log('handleMovableChange: ', e);
    let dataset = e.currentTarget.dataset;
    let currentX = this.data.currentX;
    currentX[dataset.index] = e.detail.x;
    this.setData({
      currentX
    })
  },
  bindtouchend(e) {
    // console.log("bindtouchend: ",e);
    let dataset = e.currentTarget.dataset;
    let xList = this.data.xList;
    if (this.data.currentX[dataset.index] < -80 / app.globalData.ratio) {
      xList[dataset.index] = -160 / app.globalData.ratio
    } else {
      xList[dataset.index] = 0
    }
    this.setData({
      xList
    });
  },
  handleDeleteProduct(e) {
    let that = this;
    let dataset = e.currentTarget.dataset;
    let xList = this.data.xList
    xList[dataset.index] = 0
    this.setData({
      xList
    });
    wx.showModal({
      title: '确定删除这个方案？',
      content: '删除后，客户将无法查看该方案',
      cancelText: '取消',
      cancelColor: '#999999',
      confirmText: '删除',
      confirmColor: '#f2270c',
      success(res) {
        if (res.confirm) {
          // console.log('用户点击确定');
          // 删除后数据提交后台
          /* app.wxRequest("GET", "/deleteRentPlanById.json", {
            rentPlanId: dataset.id
          }, res => { */
            // console.log("deleteRentPlanById: ", res)
            if (res.code == 200) {
              that.data.list.splice(dataset.index, 1);
              that.data.xList.splice(dataset.index, 1);
              that.setData({
                list: that.data.list,
                xList: that.data.xList,
                currentX: that.data.currentX
              })
              wx.showToast({
                title: res.msg + '' || '删除成功',
                icon: 'none',
                duration: 2000
              })
            } else {
              wx.showToast({
                title: res.msg || '删除失败',
                icon: 'none',
                duration: 2000
              })
            }
          /* }) */
        } else if (res.cancel) {
          // console.log('用户点击取消')
        }
      }
    })
  }
})